<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Maximize stopband attenuation of a linear phase lowpass FIR filter</title>
<link rel="canonical" href="http://cvxr.com/cvx/examples/filter_design/html/fir_lin_phase_lowpass_max_atten.html">
<link rel="stylesheet" href="../../examples.css" type="text/css">
</head>
<body>
<div id="header">
<h1>Maximize stopband attenuation of a linear phase lowpass FIR filter</h1>
Jump to:&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#source">Source code</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#output">Text output</a>
&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#plots">Plots</a>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="../../index.html">Library index</a>
</div>
<div id="content">
<a id="source"></a>
<pre class="codeinput">
<span class="comment">% "Filter design" lecture notes (EE364) by S. Boyd</span>
<span class="comment">% (figures are generated)</span>
<span class="comment">%</span>
<span class="comment">% Designs a linear phase FIR lowpass filter such that it:</span>
<span class="comment">% - minimizes maximum stopband attenuation</span>
<span class="comment">% - has a constraint on the maximum passband ripple</span>
<span class="comment">%</span>
<span class="comment">% This is a convex problem (when sampled it can be represented as an LP).</span>
<span class="comment">%</span>
<span class="comment">%   minimize   max |H(w)|                     for w in the stopband</span>
<span class="comment">%       s.t.   1/delta &lt;= |H(w)| &lt;= delta     for w in the passband</span>
<span class="comment">%</span>
<span class="comment">% where H is the frequency response function and variable is</span>
<span class="comment">% h (the filter impulse response). delta is allowed passband ripple.</span>
<span class="comment">%</span>
<span class="comment">% Written for CVX by Almir Mutapcic 02/02/06</span>

<span class="comment">%********************************************************************</span>
<span class="comment">% user's filter specifications</span>
<span class="comment">%********************************************************************</span>
<span class="comment">% filter order is 2n+1 (symmetric around the half-point)</span>
n = 10;

wpass = 0.12*pi;        <span class="comment">% passband cutoff freq (in radians)</span>
wstop = 0.24*pi;        <span class="comment">% stopband start freq (in radians)</span>
max_pass_ripple = 1;    <span class="comment">% (delta) max allowed passband ripple in dB</span>
                        <span class="comment">% ideal passband gain is 0 dB</span>

<span class="comment">%********************************************************************</span>
<span class="comment">% create optimization parameters</span>
<span class="comment">%********************************************************************</span>
N = 30*n;                              <span class="comment">% freq samples (rule-of-thumb)</span>
w = linspace(0,pi,N);
A = [ones(N,1) 2*cos(kron(w',[1:n]))]; <span class="comment">% matrix of cosines</span>

<span class="comment">% passband 0 &lt;= w &lt;= w_pass</span>
ind = find((0 &lt;= w) &amp; (w &lt;= wpass));    <span class="comment">% passband</span>
Lp  = 10^(-max_pass_ripple/20)*ones(length(ind),1);
Up  = 10^(max_pass_ripple/20)*ones(length(ind),1);
Ap  = A(ind,:);

<span class="comment">% transition band is not constrained (w_pass &lt;= w &lt;= w_stop)</span>

<span class="comment">% stopband (w_stop &lt;= w)</span>
ind = find((wstop &lt;= w) &amp; (w &lt;= pi));   <span class="comment">% stopband</span>
As  = A(ind,:);

<span class="comment">%********************************************************************</span>
<span class="comment">% optimization</span>
<span class="comment">%********************************************************************</span>
<span class="comment">% formulate and solve the linear-phase lowpass filter design</span>
cvx_begin
  variable <span class="string">h(n+1,1)</span>;

  minimize( max( abs( As*h ) ) )
  subject <span class="string">to</span>
    <span class="comment">% passband bounds</span>
    Lp &lt;= Ap*h;
    Ap*h &lt;= Up;
cvx_end

<span class="comment">% check if problem was successfully solved</span>
disp([<span class="string">'Problem is '</span> cvx_status])
<span class="keyword">if</span> ~strfind(cvx_status,<span class="string">'Solved'</span>)
  <span class="keyword">return</span>
<span class="keyword">else</span>
  fprintf(1,<span class="string">'The minimum attenuation in the stopband is %3.2f dB.\n\n'</span>,<span class="keyword">...</span>
          20*log10(cvx_optval));
  <span class="comment">% construct the full impulse response</span>
  h = [flipud(h(2:end)); h];
<span class="keyword">end</span>

<span class="comment">%********************************************************************</span>
<span class="comment">% plots</span>
<span class="comment">%********************************************************************</span>
figure(1)
<span class="comment">% FIR impulse response</span>
plot([0:2*n],h',<span class="string">'o'</span>,[0:2*n],h',<span class="string">'b:'</span>)
xlabel(<span class="string">'t'</span>), ylabel(<span class="string">'h(t)'</span>)

figure(2)
<span class="comment">% frequency response</span>
H = exp(-j*kron(w',[0:2*n]))*h;
<span class="comment">% magnitude</span>
subplot(2,1,1)
plot(w,20*log10(abs(H)),<span class="keyword">...</span>
     [0 wpass],[max_pass_ripple max_pass_ripple],<span class="string">'r--'</span>,<span class="keyword">...</span>
     [0 wpass],[-max_pass_ripple -max_pass_ripple],<span class="string">'r--'</span>);
axis([0,pi,-50,10])
xlabel(<span class="string">'w'</span>), ylabel(<span class="string">'mag H(w) in dB'</span>)
<span class="comment">% phase</span>
subplot(2,1,2)
plot(w,angle(H))
axis([0,pi,-pi,pi])
xlabel(<span class="string">'w'</span>), ylabel(<span class="string">'phase H(w)'</span>)
</pre>
<a id="output"></a>
<pre class="codeoutput">
 
Calling sedumi: 756 variables, 240 equality constraints
   For improved efficiency, sedumi is solving the dual problem.
------------------------------------------------------------
SeDuMi 1.21 by AdvOL, 2005-2008 and Jos F. Sturm, 1998-2003.
Alg = 2: xz-corrector, Adaptive Step-Differentiation, theta = 0.250, beta = 0.500
eqs m = 240, order n = 757, dim = 757, blocks = 1
nnz(A) = 6948 + 0, nnz(ADA) = 5844, nnz(L) = 3042
 it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
  0 :            2.72E+02 0.000
  1 :  -2.64E-01 1.54E+02 0.000 0.5670 0.9000 0.9000   8.59  1  1  7.7E+01
  2 :  -1.41E-01 6.07E+01 0.000 0.3934 0.9000 0.9000   5.26  1  1  9.9E+00
  3 :  -4.93E-02 3.86E+01 0.000 0.6360 0.9000 0.9000   3.88  1  1  2.9E+00
  4 :  -3.46E-02 2.87E+01 0.000 0.7427 0.9000 0.9000   2.55  1  1  1.7E+00
  5 :  -2.32E-02 1.56E+01 0.000 0.5430 0.9000 0.9000   1.97  1  1  7.5E-01
  6 :  -2.01E-02 7.84E+00 0.000 0.5038 0.9000 0.9000   1.37  1  1  3.5E-01
  7 :  -1.79E-02 2.57E+00 0.000 0.3285 0.9000 0.9000   1.17  1  1  1.1E-01
  8 :  -1.75E-02 8.33E-01 0.000 0.3235 0.9000 0.9000   1.04  1  1  3.5E-02
  9 :  -1.75E-02 1.83E-01 0.000 0.2192 0.9080 0.9000   1.01  1  1  8.1E-03
 10 :  -1.75E-02 3.45E-02 0.000 0.1892 0.9055 0.9000   1.00  1  1  1.6E-03
 11 :  -1.75E-02 4.21E-03 0.000 0.1219 0.9088 0.9000   1.00  1  1  2.0E-04
 12 :  -1.75E-02 2.19E-05 0.000 0.0052 0.9990 0.9990   1.00  1  1  
iter seconds digits       c*x               b*y
 12      0.1  15.4 -1.7476196636e-02 -1.7476196636e-02
|Ax-b| =   3.2e-16, [Ay-c]_+ =   5.0E-17, |x|=  6.1e-01, |y|=  3.2e-01

Detailed timing (sec)
   Pre          IPM          Post
1.000E-02    5.000E-02    0.000E+00    
Max-norms: ||b||=1, ||c|| = 1.122018e+00,
Cholesky |add|=0, |skip| = 0, ||L.L|| = 1.33849.
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +0.0174762
Problem is Solved
The minimum attenuation in the stopband is -35.15 dB.

</pre>
<a id="plots"></a>
<div id="plotoutput">
<img src="fir_lin_phase_lowpass_max_atten__01.png" alt=""> <img src="fir_lin_phase_lowpass_max_atten__02.png" alt=""> 
</div>
</div>
</body>
</html>